package com.mtz.service.impl;

import com.mtz.dateobject.UserServer;
import com.mtz.enums.ResultEnum;
import com.mtz.exception.MtzException;
import com.mtz.repository.UserServerRepository;
import com.mtz.service.UserServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 煜赜
 * @date 2018/12/4 5:16
 */
@Service
@Slf4j
public class UserServerServiceImpl implements UserServerService {

    @Autowired
    private UserServerRepository userServerRepository;

    @Override
    public List<UserServer> findAll() {
        List<UserServer> userServerList = userServerRepository.findAll();
        userServerList.remove(0);
        return userServerList;
    }

    @Override
    public UserServer findById(Integer id) {
        if (!userServerRepository.findById(id).isPresent()) {
            throw new MtzException(ResultEnum.USER_NOT_EXIST);
        }
        return userServerRepository.findById(id).get();
    }

    @Override
    public UserServer findByUsername(String username) {
        UserServer userServer = userServerRepository.findByUsername(username);
        if (userServer == null) {
            throw new MtzException(ResultEnum.USER_NOT_EXIST);
        }
        return userServer;
    }

    @Override
    public List<UserServer> findByType(Integer type) {
        return userServerRepository.findByType(type);
    }

    @Override
    public UserServer checkLogin(String username, String password) {
        UserServer userServer = userServerRepository.findByUsername(username);
        if (userServer == null) {
            throw new MtzException(ResultEnum.USER_NOT_EXIST);
        }
        if (!userServer.getPassword().equals(password)) {
            throw new MtzException(ResultEnum.PASSWORD_ERROR);
        }

        return userServer;
    }

    @Override
    public void addUser(String username, String password, Integer type) {
        UserServer userServer = new UserServer();
        userServer.setUsername(username);
        userServer.setPassword(password);
        userServer.setType(type);

        userServerRepository.save(userServer);
    }

    @Override
    public void editUser(Integer id, String username, String password) {
        UserServer userServer = userServerRepository.findById(id).get();
        userServer.setUsername(username);
        userServer.setPassword(password);
        userServerRepository.save(userServer);
    }

    @Override
    public void changeType(Integer id, Integer type) {
        UserServer userServer = userServerRepository.findById(id).get();
        userServer.setType(type);
        userServerRepository.save(userServer);
    }

    @Override
    public void changeIsDeledted(Integer id, Integer isDeleted) {
        UserServer userServer = userServerRepository.findById(id).get();
        userServer.setIsDeleted(isDeleted);
        userServerRepository.save(userServer);
    }
}
